package com.xingli.jingdongshop.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import weixin.popular.api.MenuAPI;
import weixin.popular.api.PayMchAPI;
import weixin.popular.api.TokenAPI;
import weixin.popular.bean.menu.Button;
import weixin.popular.bean.menu.MenuButtons;
import weixin.popular.bean.message.EventMessage;
import weixin.popular.bean.paymch.Sendredpack;
import weixin.popular.bean.xmlmessage.XMLTextMessage;
import weixin.popular.client.LocalHttpClient;
import weixin.popular.support.TokenManager;
import weixin.popular.util.JsonUtil;
import weixin.popular.util.SignatureUtil;
import weixin.popular.util.XMLConverUtil;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/jd/wx")
public class WxController {

    @Value("${wx.appId}")
    private String appId;
    @Value("${wx.appSecret}")
    private String appSecret;

    @Value("${wx.token}")
    private String token;

    @Value("${wx.pay.macId}")
    private String payMchId;

    @Value("${wx.pay.payKey}")
    private String payKey;

    @PostConstruct
    public void initWxConf(){
        LocalHttpClient.initMchKeyStore(payMchId,"/home/jd-shop/cert/apiclient_certsafsafas213123220212.p12");
        TokenManager.init(appId,appSecret);

    }

    @GetMapping("/message")
    public Object verifyWxMessage(String signature,String timestamp,String nonce,String echostr){
        System.out.println(signature);
        if (signature.equals(SignatureUtil.generateEventMessageSignature(token,timestamp,nonce))){
            System.out.println("验证成功 "+echostr);
            return echostr;
        }else {
            System.out.println("验证失败");
            return "";
        }
    }

    @PostMapping("/message")
    public String getWxMessage(@RequestBody String msg){
        System.out.println(msg);
        Map<String,String> map = XMLConverUtil.convertToMap(msg);
        System.out.println(map);
        String userOpenId = map.get("FromUserName");
        String Content = map.get("Content");
        String developerId = map.get("ToUserName");
        if ("红包".equals(Content)){
            String sendName = "星力云购";
            String wish = "恭喜你获得一个红包";
            String clientIp = "47.98.60.33";
            String actName = "金豆商城活动";
            String remark = "快来领取金豆";

            int money = ((int)(Math.random()*2)+100);
            Sendredpack sendredpack = new Sendredpack();
            sendredpack.setNonce_str(UUID.randomUUID().toString().replace("-",""));
            sendredpack.setMch_billno(String.valueOf(System.currentTimeMillis()));
            sendredpack.setMch_id(payMchId);
            sendredpack.setWxappid(appId);
            sendredpack.setSend_name(sendName);
            sendredpack.setRe_openid(userOpenId);
            sendredpack.setTotal_amount(money);
            sendredpack.setTotal_num(1);
            sendredpack.setWishing(wish);
            sendredpack.setClient_ip(clientIp);
            sendredpack.setAct_name(actName);
            sendredpack.setRemark(remark);
            PayMchAPI.mmpaymkttransfersSendredpack(sendredpack,payKey);
            return "success";
        }

        XMLTextMessage xmlTextMessage = new XMLTextMessage(userOpenId,developerId,Content+"=========");
        return xmlTextMessage.toXML();
    }

    @GetMapping("/menu")
    public Object addMenu(){
        MenuButtons menuButtons = new MenuButtons();
        Button button = new Button();
        button.setUrl("http://ssbox.xingli.com/hongbaoWxAuth");
        button.setType("view");
        button.setName("充值卡");
//        menuButtons.setMenuid();
        menuButtons.setButton(new Button[]{button});
//        menuButtons.setMatchrule();
        System.out.println(JsonUtil.toJSONString(menuButtons));
        MenuAPI.menuCreate(TokenManager.getToken(appId),new MenuButtons());
        return "231";
    }

//    public static void main(String[] args) {
//        System.out.println(UUID.randomUUID().toString().replace("-","").length());
//    }

}
